use axum::Router;
use tower_http::services::ServeDir;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

pub async fn static_file_serve(dir: &str, addr: &str) {
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "example_static_file_server=debug,tower_http=debug".into()),
        )
        .with(tracing_subscriber::fmt::layer())
        .init();

    tokio::join!(serve(using_serve_dir(dir), addr));
}

pub fn using_serve_dir(dir: &str) -> Router {
    // serve the file in the "assets" directory under `/assets`
    //Router::new().nest_service("/", ServeDir::new(dir))

    Router::new().fallback_service(ServeDir::new(dir))
}

async fn serve(app: Router, addr: &str) {
    //let addr = SocketAddr::from(([127, 0, 0, 1], port));
    //let addr = addr.parse::<SocketAddr>().unwrap();

    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();

    axum::serve(listener, app.into_make_service())
        .await
        .unwrap();
}
